package com.ziytek.web.citizen.service.impl;

import com.ziytek.web.citizen.config.ConfigInit;
import com.ziytek.web.citizen.service.InterruptIssuedService;
import com.ziytek.web.citizen.service.itf.StopService;
import jakarta.annotation.PreDestroy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;


/**
 * @author fenghx
 * @version v1.0
 * @className StopServiceImpl
 * @description 功能描述
 * @date 2023/8/7 15:00
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class StopServiceImpl implements StopService {

    private final ConfigInit configInit;
    private final InterruptIssuedService interruptIssuedService;

    @Override
    @PreDestroy
    public void stop() {

    }

    @EventListener(ContextClosedEvent.class)
    public void onShutdown() {
        log.info("ContextClosedEvent 服务停止时触发");

        if (configInit.isInterruptAutomatic()) {
            interruptIssuedService.interruptIssued();
        } else {
            log.info("ContextClosedEvent 服务停止时触发,不执行中断相关操作");
        }

        log.info("ContextClosedEvent 服务停止时触发结束");
    }
}
